package com.zc.basic.app.service.rpt;

import com.zc.basic.domain.dto.rpt.RptColumnNode;
import com.zc.basic.domain.pojo.rpt.RptCommonBaseInfo;
import com.zc.basic.domain.pojo.rpt.RptCommonColumn;
import com.zc.basic.infra.mapper.RptCommonBaseInfoMapper;
import com.zc.basic.infra.mapper.RptCommonColumnMapper;
import com.zc.commons.exception.MsgHandlerException;
import com.zc.commons.utils.ExcelUtil;
import com.zc.commons.utils.StringUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomRptService {

    @Autowired
    private RptCommonBaseInfoMapper baseInfoMapper;

    @Autowired
    private RptCommonColumnMapper columnMapper;

    private static final String ORG_CODE_CN = "一级支行编号";
    private static final String ORG_NAME_CN = "一级支行名称";


    public void addBaseInfo(RptCommonBaseInfo commonBaseInfo) {
        baseInfoMapper.insert(commonBaseInfo);
    }


    public void addRptColumnInfo(Workbook workbook, int rptLayer, int rptId) {
        List<RptCommonColumn> columnList = parseBaseInfoExcel(workbook, rptLayer);
        for (RptCommonColumn rptCommonColumn : columnList) {
            columnMapper.insert(rptCommonColumn);
        }

    }


    private List<RptCommonColumn> parseBaseInfoExcel(Workbook workbook, int rptLayer) {

        Sheet sheet = workbook.getSheetAt(0);
        Row firstRow = sheet.getRow(0);
        String orgCodeCn = firstRow.getCell(0).getStringCellValue();
        String orgNameCn = firstRow.getCell(1).getStringCellValue();
        if (!checkOrgColumn(orgCodeCn, orgNameCn)) {
            throw new MsgHandlerException("表头一级支行信息有误！");
        }
        Row lastRow = sheet.getRow(rptLayer);
        short lastCellNum = lastRow.getLastCellNum();
        for (int i = 0; i < rptLayer; i++) {
            Row row = i == 0 ? firstRow : sheet.getRow(i);
            Cell cell = row.getCell(2);
            String beginValue = ExcelUtil.getCellValueStr(cell);
            if (StringUtil.isNullOrEmpty(beginValue)) {
                throw new MsgHandlerException("第一个格子未检测到数据");
            }
            for (int j = 3; j < lastCellNum; j++) {
                RptColumnNode columnNode = new RptColumnNode();
                columnNode.setBegin(j - 1);
                Cell nextCell = row.getCell(j);
                String nextValue = ExcelUtil.getCellValueStr(nextCell);
                if (!StringUtil.isNullOrEmpty(nextValue)) {
                    for (int k = j + 1; k < lastCellNum; k++) {
                        columnNode.setEnd(j - 1);
                    }
                }
            }

        }


        for (int i = 0; i < rptLayer; i++) {
            Row row = i == 0 ? firstRow : sheet.getRow(i);
            String beginValue = null;
            boolean beginInit = false;
            for (int j = 2; j < lastCellNum; j++) {
                RptCommonColumn commonColumn = new RptCommonColumn();
                beginValue = beginInit ? beginValue : ExcelUtil.getCellValueStr(row.getCell(j));
                commonColumn.setColumnLevel(i + 1);
                commonColumn.setColumnName(beginValue);
                commonColumn.setColumnValue("row_" + i + "_line_" + j);
                while (++j < lastCellNum) {
                    Cell nextCell = row.getCell(j);
                    String nextValue = ExcelUtil.getCellValueStr(nextCell);
                    if (!StringUtil.isNullOrEmpty(nextValue)) {
                        beginValue = nextValue;
                        beginInit = true;
                        break;
                    }
                }
            }
        }


        return null;

    }


    private boolean checkOrgColumn(String orgCodeCn, String orgNameCn) {
        return ORG_CODE_CN.equals(orgCodeCn) && ORG_NAME_CN.equals(orgNameCn);
    }


}
